import { Action } from "./CustomType/Action";
import Singleton from "./SingleTon";


/**通知事件 */
export class NotifyEventType {
    /****************************************系统事件 ****************************************/
    public static  TOUCH_START  = cc.Node.EventType.TOUCH_START; 
    public static  TOUCH_MOVE   = cc.Node.EventType.TOUCH_MOVE;
    public static  TOUCH_END    = cc.Node.EventType.TOUCH_END;
    public static  TOUCH_CANCE  = cc.Node.EventType.TOUCH_CANCEL;
    public static  TOUCH_DOUBLE = "cc.Node.EventType.TOUCH_DOUBLE";

    /****************************************游戏事件 ****************************************/
    public static GameResume = "GameResume"; 
    public static GamePause = "GamePause"; 
    public static PropNum = "PropNum"; // 道具数量刷新


    /****************************************UI事件 ********************************************/
    
}

export default class NotifyMrg extends Singleton {
    private _mapEvents: Map<NotifyEventType, Map<string, Action>> = new Map<NotifyEventType, Map<string, Action>>();
    /**
     * 注册监听
     * @param eventType 
     * @param act 
     */
    public register(eventType: NotifyEventType, uuid: string, act: Action) {
        if (!this._mapEvents.get(eventType)) {
            this._mapEvents.set(eventType, new Map<string, Action>);
        }
        this._mapEvents.get(eventType).set(uuid, act);
        // console.log("notify register ", this._mapEvents);
    }

    /**
     * 取消监听
     * @param eventType 
     */
    public unRegister(uuid: string, eventType?: NotifyEventType) {
        if (!eventType) {
            // 注销obj所有监听
            this._mapEvents.forEach((value, key)=>{
                if (value && value.get(uuid)) {
                    value.delete(uuid);
                }
            });
        } else {
            let map = this._mapEvents.get(eventType);
            if (map) {
                map.delete(uuid);
            }
        }
    }

    /**
     * 发送通知
     * @param eventType 
     */
    public sendNotify(eventType: NotifyEventType, param?: any) {
        let maps = this._mapEvents.get(eventType);
        if (maps) {
            maps.forEach((value, key)=>{
                value.Run([param]);
            });
        }
    }
}
